package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

//c端用户
@RestController     //@ResponseBody + @Controller
public class UserController {

    //依赖注入mapper层
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * Request URL: http://localhost:8081/user/sendMsg
     * POST
     */
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String,String> param){   //接收请求体中传入的参数
        //1.获取参数
        String phone = param.get("phone");
        //2.调用service层
        userService.sendMsg(phone);
        //3.返回响应
        return ResultInfo.success();
    }

    /**
     * 登录/注册 验证
     * Request URL: http://localhost:8081/user/login
     * Request Method: POST
     */
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> param){
        //1. 获取参数
        String phone = param.get("phone");
        String code = param.get("code");
        //2. 调用service层
        ResultInfo resultInfo = userService.login(phone,code);
        //3. 返回响应
        return resultInfo;
    }

    /**
     * 退出登录
     * Request URL: http://localhost:8081/user/logout
     * Request Method: POST
     */
    @PostMapping("/user/logout")
    public ResultInfo logout(){
        //1.调用service层
        userService.logout();
        //2.返回响应
        return ResultInfo.success();
    }

}
